<?php

namespace app\man\controller;

use think\Controller;
use think\Db;
use think\Paginator;
use think\Cache;
use think\Request;
class Zb extends Common
{
	public function dlist(){
		 
		$domains = Db::name("zb")->order('paixu desc')->paginate();
		$domain = Db::name('jump')->where('status',2)->find();
		$this->assign('list',$domains);
		$this->assign('domain',$domain['domain']);
		return $this->fetch('list');
	}

	public function add(){

		return $this->fetch('add');
	}
	public function doadd()
	{

		$domains = input('domains');
		$lists = explode("\n",$domains);
		foreach($lists as $k=>$val){
			$data[$k]['domain']=trim($val);
			$data[$k]['status']=1;
		}
		$result = Db::name("ad")->insertAll($data);
		if($result){
			$this->success('添加成功', url('man/ad/add'), 3);
		}else{
			$this->success('添加失败');
		}

	}
	public function edit(){
		$aid = input('id');
		if(Request::instance()->isPost()){
			$data=Request::instance()->post();
	        $res=Db::name('zb')->where('id',$aid)->update($data);
	        if($res){
	        	$options = [
	            // 缓存类型为File
	            'type'  =>  'Redis', 
	            // 缓存有效期为永久有效
	            'expire'=>  600,
	            //缓存前缀
	            'prefix'=>  'think',
	             // 指定缓存目录
	            'host'  =>  '127.0.0.1',

	            'port'  => '6379'
	        ];
        	Cache::connect($options);
			Cache::rm("zb_list");
				$this->success('修改成功',url('man/zb/dlist'));
				
			}else{
				$this->success('修改失败',url('man/zb/dlist'));
			}
		}
		$art = Db::name('zb')->where('id',$aid)->find();
		$this->assign('art',$art);
		return $this->fetch('edit');
	}
	
	public function delete(){
		$id = input('aid');

		$res = Db::name('zb')->where('id',$id)->delete();

		if($res){
			$this->success("删除成功");
		}else{
			$this->success("删除失败");
		}
	}

	public function upField()
	{
		$aid = input('aid');
		$field=input('field');
		$val = input('val');
		$res= Db::name('zb')->where('id',$aid)->update([$field=>$val]);
		if($res){
			return true;
		}else{
			return false;
		}
	}
}